[contents] [prev] [next] [top] [bottom] (2 out of 5)

Coercing Objects to Other Classes

Object coercion refers to the ability to convert an instance of one class into an instance of another. For example, you might coerce an integer (an instance of ImmediateInteger) into its string representation (an instance of String, StringConstant, or Text). In ScriptX, however, object coercion does not change the original object, as the term coerce might imply. Instead, coercion in ScriptX creates a new instance of the target class, and uses the original object for deriving the contents of that new instance.

The as expression coerces an object to another class:

object as class
In this expression, object is the object you want to coerce, and class is the class you want the object to be coerced to. The types of classes a given object can be coerced to is determined by the destination class, or if the destination class can't coerce, the object's own class. If neither attempt at coercion succeeds, an exception is reported.

123 as Float
123.0
123 as String
"123"
123 -- the original object is still 123
123

myList := new LinkedList
myList as Fixed -- this attempt to coerce reports an exception
-- ** Cannot coerce #() as LinkedList into a Fixed. (CantCoerce)

When an object is coerced to another class, some of the information the original object contained can be lost. For example, when you convert an instance of a number class with more precision to one with less precision, you may lose the extra information that the additional precision provided:

43.5 as ImmediateInteger
43

In this example, the original object (an instance of ImmediateFloat) was coerced to the class ImmediateInteger, which truncated the fractional part.

Table 3-1 provides some simple examples of how objects can be coerced.

Table 3-1: Class coercion examples

Original Class

Sample Expression

Result

ImmediateInteger

123 as Float

123.0

ImmediateInteger

123 as LargeInteger

123

ImmediateInteger

123 as String

"123"

ImmediateInteger

123 as Array

#(123)

Float

123.5 as ImmediateInteger

123

String

"345" as ImmediateInteger

345

String

"345" as Fixed

345.0

String

"red" as Array

#("r","e","d")

Note that in order to specify most complex expressions on either side of the as construct you must specify those expressions inside parentheses so that they are evaluated first. The only expressions that can be used without parentheses in an as expression are, where appropriate:

A coercion expression is actually compiled as a call to the global function coerce. A script can call coerce as an alternative to the as operator. For example, the following expressions coerce a StringConstant object, that is a string literal, to a Text object, a kind of string that can be edited. These two coercion expressions are equivalent:

"When in the course of human events" as Text
coerce "When in the course of human events" Text

The coerce function, in turn, calls one of two underlying generic functions. The Coercion protocol comprises a set of generic functions that allow scripted classes to be coerced to and from other classes, including substrate classes. Each class can implement its own version of morph or newFrom to define which classes of objects it can be coerced to, and to perform the coercion operation. By specializing these generics, scripted classes can be coerced freely using the as construct in the ScriptX language. If an object cannot be converted, it reports the cantCoerce exception. For more information on coercion, see the "Object System Kernel" chapter of the ScriptX Components Guide.


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.